package cn.com.ddapp.ddm.ui.mine;

import android.content.Intent;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.MainActivity;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.mine.UserData;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.util.NetUtils;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.util.TDevice;
import okhttp3.Call;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.alter_password)
    AutoRelativeLayout alterPassword;
    @BindView(R.id.pay_password)
    AutoRelativeLayout payPassword;
    @BindView(R.id.phone_number)
    AutoRelativeLayout phoneNumber;
    @BindView(R.id.email)
    AutoRelativeLayout email;
    @BindView(R.id.certification)
    AutoRelativeLayout certification;
    @BindView(R.id.logout)
    Button logout;
    @BindView(R.id.error_layout)
    EmptyLayout mEmptyLayout;

    String userId;
    OkUtilCallback callback;
    //横竖屏
    int orientation;

    @Override
    public void initView() {
        titletext.setText(R.string.setting);
        //判断是否横屏,1竖屏0横屏
        orientation = getResources().getConfiguration().orientation;
        if (orientation == 0 || orientation == 2) {
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenHeigh = dm.heightPixels;
            ViewGroup.LayoutParams params = alterPassword.getLayoutParams();
            params.height = screenHeigh / 10;
            alterPassword.setPadding(80, 0, 80, 0);
            payPassword.setPadding(80, 0, 80, 0);
            phoneNumber.setPadding(80, 0, 80, 0);
            email.setPadding(80, 0, 80, 0);
            certification.setPadding(80, 0, 80, 0);
            alterPassword.setLayoutParams(params);
            payPassword.setLayoutParams(params);
            phoneNumber.setLayoutParams(params);
            email.setLayoutParams(params);
            certification.setLayoutParams(params);
            LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) logout.getLayoutParams();
            params1.height = screenHeigh / 10;
            params1.leftMargin = 40;
            params1.rightMargin = 40;
            logout.setLayoutParams(params1);
        }
    }

    @Override
    public void initData() {
        userId = SPUtils.getStringData(this, SPUtils.USERID);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.alter_password, R.id.pay_password, R.id.phone_number, R.id.email, R.id.certification, R.id.back, R.id.logout})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
        }
        if (NetUtils.isConnected(this) || NetUtils.isWifi2(this)) {
            Intent intent = null;
            switch (view.getId()) {
                //修改登录密码
                case R.id.alter_password:
                    intent = new Intent(this, LoginPasswordActivity.class);
                    startActivity(intent);
                    break;

                //修改或设置支付密码
                case R.id.pay_password:
                    callback = new OkUtilCallback() {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            UserData userData = gson.fromJson(response, UserData.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                                BaseApplication.getIntstance().showToast(userData.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                                Intent intent = null;
                                //有支付密码，修改支付密码
                                if (userData.isPayPwdIfSet()) {
                                    intent = new Intent(SettingActivity.this, AlterPwdActivity.class);
                                } else {
                                    intent = new Intent(SettingActivity.this, SettingPwdActivity.class);
                                }
                                startActivity(intent);
                            }
                        }

                        @Override
                        public void onError(Call call, Exception e, int id) {
                            super.onError(call, e, id);
                            BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                        }
                    };
                    OKUtilAPI.post(OKUtilAPI.getUser, "userId=" + userId, callback);
                    break;

                case R.id.phone_number:
                    callback = new OkUtilCallback() {
                        @Override
                        public void onMyResponse(String response, int id) {
                            Gson gson = new Gson();
                            UserData userData = gson.fromJson(response, UserData.class);
                            if ((!AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                                BaseApplication.getIntstance().showToast(userData.getResult(), Toast.LENGTH_SHORT, 0, Gravity.TOP);
                            } else if ((AppConfig.RESULT_OK.equalsIgnoreCase(userData.getResult()))) {
                                Intent intent = null;
                                //没有绑定手机号码
                                if ("".equals(userData.getUser().getMobile())) {
                                    intent = new Intent(SettingActivity.this, BindMobileActivity.class);
                                } else {
                                    //修改手机号码
                                    intent = new Intent(SettingActivity.this, AlterBindMobileActivity.class);
                                }
                                startActivity(intent);
                            }
                        }

                        @Override
                        public void onError(Call call, Exception e, int id) {
                            super.onError(call, e, id);
                            BaseApplication.getIntstance().showToast(R.string.network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
                        }
                    };
                    OKUtilAPI.post(OKUtilAPI.getUser, "userId=" + userId, callback);
                    break;
                case R.id.email:
                    intent = new Intent(this, EmailActivity.class);
                    startActivity(intent);
                    break;
                case R.id.certification:
                    if (TDevice.isTablet(getApplicationContext())) {
                        intent = new Intent(this, CertificationActivityAtm.class);
                        startActivity(intent);
                    } else {
                        intent = new Intent(this, CertificationActivity.class);
                        startActivity(intent);
                    }
                    break;
                case R.id.logout:
                    SPUtils.setStringData(SettingActivity.this, SPUtils.USERID, "");
                    SPUtils.setBooleanData(SettingActivity.this, SPUtils.ISLOGIN, false);
                    SPUtils.setStringData(SettingActivity.this, SPUtils.EMAIL, "");
                    intent = new Intent(SettingActivity.this, MainActivity.class);
                    startActivity(intent);
//                    startActivityForResult(intent,AppConfig.LOGOUT);
                    Intent a = new Intent();
                    a.setAction("logout");
                    sendBroadcast(a);
                    break;
            }
        } else {
            BaseApplication.getIntstance().showToast(R.string.tip_network_error, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
        }
    }
}
